package com.kanshu.ksgb.fastread.doudou.module.personal.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.BonusPool
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.ExchangeResult
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_bonus_open.*

@Route(path = ARouterConfig.PERSONAL_BONUS_OPEN)
class BonusOpenActivity :BaseActivity() {

   lateinit var bonusPool: BonusPool
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bonus_open)
        var tmp = intent.getParcelableExtra<BonusPool>("data")
        if(tmp == null){
            finish()
            return
        }else{
            bonusPool = tmp
        }
        title = "分红池"
        val rmbFen =  bonusPool?.residue_amount?.toInt() ?: 0
        val rmbYuan = formatYuan(rmbFen)
        countView.text = rmbYuan.total
        txt2.text = rmbYuan.unit
        beanCount.text = String.format(getString(R.string.left_bean_format), bonusPool.account_info?.beans_balance?:"0")
        btn_change.setOnClickListener {
            val changeCount = input.text.toString()
            if(TextUtils.isEmpty(changeCount) || changeCount.length > 9){
                ToastUtil.showMessage("请输入有效的兑换数量")
                return@setOnClickListener
            }

            doChange(changeCount.toInt())}
    }

    override fun initStatusbar() {
        super.initStatusbar()
        mImmersionBar.keyboardEnable(true)
    }

    private fun doChange(beans: Int) {
        if (beans > bonusPool.account_info?.beans_balance?.toInt() ?: 0) {
            ToastUtil.showMessage("可兑换金豆小于输入数量")
            return
        }

        val params = mutableMapOf<String, String>()
        params.put("beans",beans.toString())
        val retrofitHelper = RetrofitHelper.getInstance()
        retrofitHelper.createService(PersonCenterService::class.java)
                .exchangeBeans(params)
                .compose(asyncRequest())
                .subscribe(object: Observer<BaseResult<ExchangeResult>>{
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseResult<ExchangeResult>?) {
                        val code = t?.result?.status?.code ?: -1
                        val result = t?.result?.data;
                        if(code != 0 || result == null){
                            ToastUtil.showMessage("兑换失败: ${t?.result?.status?.msg}")
                            return
                        }
                        if(result != null){
                            switchSuccess(result)
                        }

                    }

                    override fun onError(e: Throwable?) {
                        ToastUtil.showMessage("兑换失败")
                        e?.printStackTrace()
                    }
                })


    }

    private fun switchSuccess(result:ExchangeResult){
        input.visibility = View.GONE
        btn_change.visibility = View.GONE
        title_count.setText(R.string.title_change_success)
        countView.text =  String.format("%1$.2f",result.amount.toFloat()/100)
        val left = bonusPool.account_info.beans_balance.toInt() - result.beans
        beanCount.text = String.format(getString(R.string.left_bean_format), left.toString())
    }

    companion object {
        @JvmStatic
        fun aciontStart(activity:Activity, bonusPool: BonusPool?){
            if(bonusPool == null){
                return
            }
            val intent = Intent(activity,BonusOpenActivity::class.java)
            intent.putExtra("data",bonusPool)
            activity.startActivity(intent)
        }

    }


}
